<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class WechatMenu extends Model
{
    use ModelTree, AdminBuilder;
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setTitleColumn('name');
    }

    public function typeArray($type = null)
    {
        $arr = [
            'p' => '主菜单',
            'view' => "网页",
            'miniprogram' => "小程序",
        ];
        if ($type !== null) {
            return array_key_exists($type, $arr) ? $arr[$type] : $arr['p'];
        }
        return $arr;
    }

    public static function selectRoot()
    {
        $options = WechatMenu::where('parent_id', 0)->orderby('order')->get()->toArray();
        if (!empty($options)) {
            $options = (new static())->buildSelectOptions($options);
        }

        return collect($options)->prepend('根目录', 0)->all();
    }
}
